package cn.iocoder.yudao.module.agri.controller.admin.farmrecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.agri.controller.admin.farmrecord.vo.*;
import cn.iocoder.yudao.module.agri.dal.dataobject.farmrecord.FarmRecordDO;
import cn.iocoder.yudao.module.agri.convert.farmrecord.FarmRecordConvert;
import cn.iocoder.yudao.module.agri.service.farmrecord.FarmRecordService;

@Tag(name = "管理后台 - 农事记录")
@RestController
@RequestMapping("/agri/farm-record")
@Validated
public class FarmRecordController {

    @Resource
    private FarmRecordService farmRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建农事记录")
    @PreAuthorize("@ss.hasPermission('agri:farm-record:create')")
    public CommonResult<Long> createFarmRecord(@Valid @RequestBody FarmRecordCreateReqVO createReqVO) {
        return success(farmRecordService.createFarmRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新农事记录")
    @PreAuthorize("@ss.hasPermission('agri:farm-record:update')")
    public CommonResult<Boolean> updateFarmRecord(@Valid @RequestBody FarmRecordUpdateReqVO updateReqVO) {
        farmRecordService.updateFarmRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除农事记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('agri:farm-record:delete')")
    public CommonResult<Boolean> deleteFarmRecord(@RequestParam("id") Long id) {
        farmRecordService.deleteFarmRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得农事记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('agri:farm-record:query')")
    public CommonResult<FarmRecordRespVO> getFarmRecord(@RequestParam("id") Long id) {
        FarmRecordDO farmRecord = farmRecordService.getFarmRecord(id);
        return success(FarmRecordConvert.INSTANCE.convert(farmRecord));
    }

    @GetMapping("/list")
    @Operation(summary = "获得农事记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('agri:farm-record:query')")
    public CommonResult<List<FarmRecordRespVO>> getFarmRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<FarmRecordDO> list = farmRecordService.getFarmRecordList(ids);
        return success(FarmRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得农事记录分页")
    @PreAuthorize("@ss.hasPermission('agri:farm-record:query')")
    public CommonResult<PageResult<FarmRecordRespVO>> getFarmRecordPage(@Valid FarmRecordPageReqVO pageVO) {
        PageResult<FarmRecordDO> pageResult = farmRecordService.getFarmRecordPage(pageVO);
        return success(FarmRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出农事记录 Excel")
    @PreAuthorize("@ss.hasPermission('agri:farm-record:export')")
    @OperateLog(type = EXPORT)
    public void exportFarmRecordExcel(@Valid FarmRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FarmRecordDO> list = farmRecordService.getFarmRecordList(exportReqVO);
        // 导出 Excel
        List<FarmRecordExcelVO> datas = FarmRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "农事记录.xls", "数据", FarmRecordExcelVO.class, datas);
    }

}
